package com.yuliang.ai.service.Impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuliang.ai.domain.Dto.HumanCollectionDto;
import com.yuliang.ai.domain.Dto.PageDto;
import com.yuliang.ai.domain.Vo.HumanCollectionVo;
import com.yuliang.ai.domain.entity.HumanCollection;
import com.yuliang.ai.mapper.HumanCollectionMapper;
import com.yuliang.ai.service.HumanCollectionService;
import com.yuliang.utils.result.ResultUtils;
import com.yuliang.utils.result.emums.HttpCodeEnum;
import com.yuliang.utils.superbackage.SuperServiceImpl;
import com.yuliang.utils.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Huangkai
 */
@Slf4j
@Service
public class HumanCollectionServiceImpl extends SuperServiceImpl<HumanCollectionMapper, HumanCollection> implements HumanCollectionService{

    @Autowired
    HumanCollectionService humanCollectionService;

    @Autowired
    HumanCollectionMapper humanCollectionMapper;

    @Override
    public PageDto getHumanCollectionList(HumanCollectionVo humanCollectionVo) {
        PageDto pageDto = new PageDto();
        LambdaQueryWrapper<HumanCollection> wrapper = new QueryWrapper<HumanCollection>().lambda();
        wrapper.eq(HumanCollection::getUserId, humanCollectionVo.getUserId());
        wrapper.orderByDesc(HumanCollection::getCreateTime);
        wrapper.orderByDesc(HumanCollection::getId);
        //分页获取
        Page<HumanCollection> page = new Page<>();
        page.setCurrent(humanCollectionVo.getPage());
        page.setSize(humanCollectionVo.getLimit());
        IPage<HumanCollection> humanCollectionIPage = humanCollectionMapper.selectPage(page, wrapper);
        if(humanCollectionIPage.getRecords() != null){
            List<HumanCollection> humanCollectionList = humanCollectionIPage.getRecords();
            String jsonArrayStr = JSONArray.toJSONString(humanCollectionList);
            List<HumanCollectionDto>  humanCollectionDtoList = JSONArray.parseArray(jsonArrayStr, HumanCollectionDto.class);

            pageDto.setRecords(humanCollectionDtoList);
            pageDto.setCurrent(page.getCurrent());
            pageDto.setSize(page.getSize());
            pageDto.setTotal(page.getTotal());
            return pageDto;
        }
        pageDto.setRecords(new ArrayList());
        return pageDto;
    }

    @Override
    public String insertHumanCollection(HumanCollectionVo humanCollectionVo) {
        HumanCollection humanCollection = new HumanCollection();
        humanCollection.setUuid(StringUtils.getSnowFlakeId());
        humanCollection.setUserId(humanCollectionVo.getUserId());
        humanCollection.setHumanIdentifyId(humanCollectionVo.getHumanIdentifyId());
        humanCollection.insert();
        return ResultUtils.ResultWithMessageEnum(HttpCodeEnum.insert_success);
    }

    @Override
    public String delHumanCollection(HumanCollectionVo humanCollectionVo) {
        LambdaQueryWrapper<HumanCollection> wrapper = new QueryWrapper<HumanCollection>().lambda();
        wrapper.eq(HumanCollection::getUserId, humanCollectionVo.getUserId());
        wrapper.eq(HumanCollection::getHumanIdentifyId, humanCollectionVo.getHumanIdentifyId());
        humanCollectionMapper.delete(wrapper);
        return ResultUtils.ResultWithMessageEnum(HttpCodeEnum.cancel_success);
    }
}
